#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os

import yaml

from common.logcof import log
from configs.path_conf import path_config


class DoYaml(object):

    @staticmethod
    def read_yaml(filename) -> dict:
        """
        读取yaml文件数据

        :return: dict[Any] if element_file=False else {variable_name: element, ...}
        """
        try:
            with open(filename, encoding='utf-8') as f:
                data = yaml.load(f.read(), Loader=yaml.FullLoader)
                if data:
                    pass
                else:
                    raise ValueError('❌ Element file data is empty')
        except FileNotFoundError as e:
            log.error(f'❌ Read yaml file error: {e}')
            raise e
        return data

    @staticmethod
    def write_yaml(filename, data, encoding='uft-8', mode='w'):
        """
        写入yaml数据

        :param filename:
        :param data: 写入数据
        :param encoding: 文件编码格式
        :param mode: 写入模式
        :return:
        """
        try:
            with open(filename, encoding=encoding, mode=mode) as f:
                return yaml.dump(data, stream=f, allow_unicode=True)
        except FileNotFoundError as e:
            log.error(f'❌ Write yaml file error: {e}')
            raise e

if __name__ == '__main__':
    read_yaml = DoYaml.read_yaml(os.path.join(path_config.data_dir, 'login_data.yaml'))
    print(read_yaml.get('login_page'))
    print(type(read_yaml.get('login_page')))
    print(read_yaml)
